package com.zenithmind.library.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 图书评价实体
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(name = "图书评价实体", description = "图书评价信息")
@TableName("lib_book_review")
public class BookReview extends BaseEntity {

    @NotBlank(message = "用户ID不能为空")
    @Schema(description = "用户ID")
    private String userId;

    @NotBlank(message = "图书ID不能为空")
    @Schema(description = "图书ID")
    private String bookId;

    @NotNull(message = "评分不能为空")
    @Min(value = 1, message = "评分不能小于1")
    @Max(value = 5, message = "评分不能大于5")
    @Schema(description = "评分 (1-5)")
    private Integer rating;

    @Schema(description = "评价内容")
    private String reviewContent;

    @Schema(description = "是否匿名 (0-否, 1-是)")
    private Integer isAnonymous;

    @Schema(description = "状态 (0-隐藏, 1-显示)")
    private Integer status;

    @Schema(description = "点赞数")
    private Integer likeCount;

    /**
     * 获取评分星级显示
     */
    public String getRatingStars() {
        if (rating == null) {
            return "☆☆☆☆☆";
        }
        StringBuilder stars = new StringBuilder();
        for (int i = 1; i <= 5; i++) {
            if (i <= rating) {
                stars.append("★");
            } else {
                stars.append("☆");
            }
        }
        return stars.toString();
    }

    /**
     * 判断是否为好评
     */
    public boolean isPositiveReview() {
        return rating != null && rating >= 4;
    }

    /**
     * 判断是否为中性评价
     */
    public boolean isNeutralReview() {
        return rating != null && rating == 3;
    }

    /**
     * 判断是否为差评
     */
    public boolean isNegativeReview() {
        return rating != null && rating <= 2;
    }

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "隐藏";
            case 1 -> "显示";
            default -> "未知";
        };
    }

    /**
     * 判断是否显示
     */
    public boolean isVisible() {
        return status != null && status == 1;
    }

    /**
     * 判断是否匿名评价
     */
    public boolean isAnonymousReview() {
        return isAnonymous != null && isAnonymous == 1;
    }

    /**
     * 判断是否有评价内容
     */
    public boolean hasContent() {
        return reviewContent != null && !reviewContent.trim().isEmpty();
    }

    /**
     * 获取评价类型描述
     */
    public String getReviewTypeDescription() {
        if (isPositiveReview()) {
            return "好评";
        } else if (isNeutralReview()) {
            return "中评";
        } else if (isNegativeReview()) {
            return "差评";
        } else {
            return "未知";
        }
    }

    /**
     * 增加点赞数
     */
    public void incrementLikeCount() {
        likeCount = (likeCount == null ? 0 : likeCount) + 1;
    }

    /**
     * 减少点赞数
     */
    public void decrementLikeCount() {
        if (likeCount != null && likeCount > 0) {
            likeCount--;
        }
    }
}
